package com.ruyuan.commodity.common.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruyuan.commodity.model.goods.dos.GoodsParamsDO;
import com.ruyuan.commodity.model.goods.vo.GoodsParamsVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;

/**
 * GoodsParams的Mapper
 */
@Repository
public interface GoodsParamsMapper extends BaseMapper<GoodsParamsDO> {

    /**
     * 查询商品关联的参数及参数值
     * @param categoryId 分类id
     * @param goodsId 商品id
     * @return
     */
    @ResultType(GoodsParamsVO.class)
    @Select("select p.param_id, " +
                "p.param_type, " +
                "p.options," +
                "p.param_name, " +
                "p.is_index, " +
                "p.required, " +
                "p.category_id, " +
                "gp.param_value," +
                "p.group_id, " +
                "p.sort as sort " +
            "from es_parameters p left join es_goods_params gp " +
            "on p.param_id=gp.param_id and gp.goods_id = #{goods_id}  " +
            "where p.category_id = #{category_id} order by sort")
    List<GoodsParamsVO> queryGoodsParamsValue(@Param("category_id") Long categoryId, @Param("goods_id")Long goodsId);

    /**
     * 查询商品关联的可检索的参数值
     * @param ids id集合
     * @return
     */
    @ResultType(value = Map.class)
    @Select("<script>" +
            "select gp.* from es_goods_params gp inner join es_parameters p on gp.param_id=p.param_id" +
            "        <where>" +
            "            <if test='params != null'>" +
            "                goods_id in" +
            "                <foreach collection='params' index='index' item='item' open='(' separator=',' close=')'>" +
            "                    #{item}" +
            "                </foreach>" +
            "            </if>" +
            "            and is_index = 1" +
            "        </where>" +
            "</script>")
    List<Map<String, Object>> getIndexGoodsList(@Param("params") List<Object> ids);
}
